const express = require('express')
const multer = require('multer')
const router = express.Router()
const fs = require('fs')
const pool = require('./pool');
const path = require('path')
//注册
router.post('/up', function(req, res) {
	var json = req.body
	console.log(json)
	pool.mypool({
		arr: [json.user],
		sql: 'select user from login where user=?',
		success(data) {
			if(data.length) {
				res.send('账号已存在')
			} else {
				pool.mypool({
					arr: [json.user, json.pass, json.name, json.img],
					sql: 'insert into login(user,pass,name,img) values(?,?,?,?)',
					success(data) {
						res.send('注册成功')
					},
					error(err) {
						res.send(err)
					}
				})
			}
		},
		error(err) {
			res.send(err)
		}
	})
})
//登录
router.post('/in', function(req, res) {
	var json = req.body
	pool.mypool({
		arr: [json.user, json.pass],
		sql: 'select * from login where user=? and pass=?',
		success(data) {
			if(data.length) {
				data[0].pass = ''
				res.send(data[0])
			} else {
				res.send('账号或密码错误')
			}
		},
		error(err) {
			res.send(err)
		}

	})
})
module.exports = router;
